package cn.xiaoyu.su.service.impl;

import cn.xiaoyu.su.controller.request.LinkGroupAddRequest;
import cn.xiaoyu.su.controller.request.LinkGroupUpdateRequest;
import cn.xiaoyu.su.interceptor.LoginInterceptor;
import cn.xiaoyu.su.manager.LinkGroupManager;
import cn.xiaoyu.su.model.LinkGroupDO;
import cn.xiaoyu.su.mapper.LinkGroupMapper;
import cn.xiaoyu.su.model.LoginUser;
import cn.xiaoyu.su.service.LinkGroupService;
import cn.xiaoyu.su.vo.LinkGroupVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ⼆当家⼩D
 * @since 2022-12-03
 */
@Service
@Slf4j
public class LinkGroupServiceImpl implements LinkGroupService {

    @Autowired
    private LinkGroupManager linkGroupManager;

    /**
     * 创建分组
     * @param linkGroupAddRequest
     * @return
     */
    @Override
    public int add(LinkGroupAddRequest linkGroupAddRequest) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(linkGroupAddRequest.getTitle());
        linkGroupDO.setAccountNo(accountNo);

        int rows = linkGroupManager.add(linkGroupDO);

        return rows;
    }

    /**
     * 删除分组
     * @param groupId
     * @return
     */
    @Override
    public int del(long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        int rows = linkGroupManager.del(groupId,accountNo);

        return rows;
    }

    /**
     * 查询分组详情
     * @param groupId
     * @return
     */
    @Override
    public LinkGroupVO detail(long groupId) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId,accountNo);

        if (linkGroupDO == null) {
            return null;
        }

        LinkGroupVO linkGroupVO = new LinkGroupVO();

        //复杂的可以用mapStruct，这里用BeanUtils
        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;
    }

    /**
     * 查询用户所有分组
     * @return
     */
    @Override
    public List<LinkGroupVO> list() {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        List<LinkGroupDO> linkGroupDOS = linkGroupManager.list(accountNo);

        if (CollectionUtils.isEmpty(linkGroupDOS)) {
            return null;
        }

        List<LinkGroupVO> linkGroupVOS = linkGroupDOS.stream().map(obj -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(obj, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());

        return linkGroupVOS;
    }

    /**
     * 更新分组名
     * @param updateRequest
     * @return
     */
    @Override
    public int update(LinkGroupUpdateRequest updateRequest) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = linkGroupManager.detail(updateRequest.getId(),accountNo);

        if (linkGroupDO == null) {
            return 0;
        }

        linkGroupDO.setTitle(updateRequest.getTitle());

        return linkGroupManager.update(linkGroupDO);
    }
}
